import React from 'react';
import { observer, inject } from 'mobx-react';
import CodeElement from './CodeElement';
import WfInput from '../../public/wf-form-input/index';
import * as Util from '../../util/formUtil';
import { _ } from '../../util/pcPublic';
import { toJS } from 'mobx';

@inject('wfGlobalStore','wfLayoutStore')
@observer
export default class InputElement extends React.Component{
    doChangeEvent(value){
        const { fieldMark,wfLayoutStore } = this.props;
		wfLayoutStore.changeSingleFieldValue(fieldMark, {value: value});
    }
    doDisablePromptEvent(){
        const { fieldMark } = this.props;
        Util.showPrompt(fieldMark, 2);
    }
    render() {
        const { fieldid,fieldMark,fieldValue,symbol,rowIndex,viewAttr,cellObj,fieldObj,fieldAttr,wfLayoutStore } = this.props;
        const { variable } = fieldObj;
        const specialobj  = fieldObj.specialobj ? toJS(fieldObj.specialobj) : {};
        //判断是否为流程编号
        const { codeInfo,linkageCfg } = wfLayoutStore;
        const { isUse,fieldCode } = codeInfo;
        if(isUse && fieldCode === fieldid){
            return <CodeElement {...this.props} />
        }
        const { detailtype,isDetail,qfws,length,fieldlabel } = fieldAttr;
        //扩展组件参数
        let inputProps = { fieldid,detailtype,isDetail,qfws,length,fieldlabel,fieldMark,viewAttr,value:fieldValue };
        if(viewAttr !== 1){   //文本类型字段空值显示的默认值
            inputProps.emptyShowContent = variable.get("emptyShowContent") || "";
        }
        if("showhtml" in specialobj){
            inputProps.showhtml = specialobj.showhtml;   //文本显示值
        }
        let format = cellObj.format || {};
        const transtype = parseInt(_.get(linkageCfg, ["transTypeCfg", fieldid]) || 0);   //字段赋值转格式，模拟用格式化实现
        const enableInput = !this.judgeBelRowCalAssignField();      //判断当前字段是否为行规则计算赋值字段，不允许修改
        if(transtype === 1){    //转金额大写
            format = { numberType:101 };
        }else if(transtype === 2){      //转金额千分位，模拟数值格式化
            format = { numberType:2, decimals:qfws, formatPattern:2, thousands:1 };
        }
        if("financial" in cellObj){      //财务格式优先级最高
            const financial = cellObj.financial || "";
            if(financial.indexOf("2-") > -1){
                const finaNum = parseInt(financial.split("-")[1] || "3");
                format = { numberType:100, finaNum:finaNum };
            }else if(financial === "3"){        //转金额大写
                format = { numberType:101 };
            }else if(financial === "4"){        //转金额千分位，模拟数值格式化两位
                format = { numberType:2, decimals:2, formatPattern:2, thousands:1 };
            }
        }
        inputProps.enableInput = enableInput;
        inputProps.format = format;

        return(
            <WfInput
                {...inputProps}
                onChange={this.doChangeEvent.bind(this)}
                onDisablePrompt={this.doDisablePromptEvent.bind(this)}
            />
        )
    }
    //判断字段是否属于明细行规则赋值字段
    judgeBelRowCalAssignField(){
        const { wfLayoutStore } = this.props;
        const { linkageCfg,cellInfo } = wfLayoutStore;
        const { fieldid,fieldAttr } = this.props;
        if(!fieldAttr.isDetail || !_.has(linkageCfg, ["rowCalCfg", "rowCalAssignInfo", fieldid]))
            return false;
        let isValid = true;
        _.get(linkageCfg, ["rowCalCfg", "rowCalAssignInfo", fieldid]).map(id =>{
            if(!_.has(cellInfo, ["fieldCellInfo", id]) || fieldAttr.viewattr <= 0)
                isValid = false;    //取值字段不在模板上认为是无效行规则
        });
        return isValid;
    }
}